﻿Public Class ctrContato
    Inherits ctrPadrao

#Region "Declaração de variáveis"
    Private objContato As New Persistencia.perContato
    Private objDepartamento As New Persistencia.perDepartamento
    Private iIDContato As Int32
    Private iIDCliente As Int32
#End Region

#Region "Propriedades"

    Public Property IDContato() As Int32
        Get
            Return Me.iIDContato
        End Get
        Set(ByVal value As Int32)
            Me.iIDContato = value
        End Set
    End Property
    Public WriteOnly Property IDCliente() As Int32
        Set(ByVal value As Int32)
            Me.iIDCliente = value
        End Set
    End Property
    Public ReadOnly Property sqlConsulta(ByVal IDCliente As Int32) As String
        Get
            Return Me.objContato.sqlConsulta(IDCliente)
        End Get
    End Property

#End Region

#Region "Métodos públicos"

    Public Function salvarContato(ByVal iIDContato As Int32, _
                           ByVal iIDCliente As Int32, _
                           ByVal sNomeContato As String, _
                           ByVal iIDDepto As Int32, _
                           ByVal sTelefone As String, _
                           ByVal sCelular As String, _
                           ByVal sFax As String, _
                           ByVal sEmail As String, _
                           ByVal bPrincipal As Boolean) As Boolean

        Dim bResultado As Boolean = False
        Dim drPrincipal As DataRow

        Try
            If (bPrincipal) Then
                drPrincipal = objContato.retornaContatoPrincipal(0, iIDCliente)
                If (Not drPrincipal Is Nothing) Then
                    If (drPrincipal.Item("IDContato") <> iIDContato) Then
                        'Se o cliente já tiver outro contato como contato principal, confirma a alteração de contato principal.
                        If (MsgBox("O contato " & drPrincipal.Item("nomeContato") & _
                               " é o contato principal deste cliente. Deseja alterar o contato principal para " & sNomeContato & "?", _
                               MsgBoxStyle.YesNo, "Aviso") = MsgBoxResult.Yes) Then

                            Call objContato.desmarcarPrincipal(iIDCliente)
                        Else
                            bPrincipal = False
                        End If

                    End If
                End If
            End If

            If iIDContato > 0 Then
                Me.alterarContato(iIDContato, _
                                  iIDCliente, _
                                  sNomeContato, _
                                  iIDDepto, _
                                  sTelefone, _
                                  sCelular, _
                                  sFax, _
                                  sEmail, _
                                  bPrincipal)

                Me.IDContato = iIDContato
                bResultado = True
            Else
                Me.IDContato = Me.inserirContato(sNomeContato, _
                                                iIDCliente, _
                                                iIDDepto, _
                                                sTelefone, _
                                                sCelular, _
                                                sFax, _
                                                sEmail, _
                                                bPrincipal)

                bResultado = True
            End If
        Catch ex As Exception
            Throw New Exception("Exceção no método 'salvarContato'." & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

        Return bResultado

    End Function

    Public Sub excluirContato(ByVal iIdContato As Int32)

        Me.objContato.excluirContato(iIdContato)

    End Sub

    Public Function selecionarContato(ByVal iIdContato As Int32, _
                                      ByVal iIdCliente As Int32, _
                                      ByVal bPrincipal As Boolean) As DataSet
        Try
            Return Me.objContato.selecionarContato(iIdContato, iIdCliente, bPrincipal)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarContato'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try
    End Function
    Public Function retornarCampoContato(ByVal sCampo As String, ByVal iIDContato As Int32) As DataRow

        Try
            Dim dtDados As New DataTable
            Dim vRetorno As DataRow

            dtDados = Me.objContato.selecionarCampoContato(sCampo, iIDContato)

            If Not dtDados Is Nothing Then
                vRetorno = dtDados.Rows(0)
            Else
                vRetorno = Nothing
            End If

            Return vRetorno

        Catch ex As Exception

            Throw New Exception("Exceção no método 'retornarCampoContato'. " & Environment.NewLine & Environment.NewLine & ex.Message)

        End Try

    End Function

    Public Function verificarContatoDepartamento(ByVal iIdDepartamento As Int32) As Boolean
        Try
            Dim dtDados As New DataTable
            Dim drDados As DataRow
            Dim bRetorno As Boolean

            dtDados = Me.objContato.verificarContatoDepartamento(iIdDepartamento)

            If Not dtDados Is Nothing Then
                For Each drDados In dtDados.Rows
                    If drDados.Item("IDDepartamento") <> 0 Then
                        bRetorno = True
                    Else
                        bRetorno = False
                    End If
                Next
            End If

            Return bRetorno
        Catch ex As Exception
            Throw New Exception("Exceção no método 'verificarContatoDepartamento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try
    End Function
    Private Function controleContatoPrincipal(ByVal iIDCliente As Int32, ByVal iIDContato As Int32) As DataRow
        Dim dsDados As DataSet

        Try
            dsDados = objContato.selecionarContato(0, iIDCliente, True)

            If (Not dsDados Is Nothing) Then
                If (dsDados.Tables(0).Rows.Count > 0) Then
                    Return dsDados.Tables(0).Rows(0)
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If
        Catch ex As Exception
            Throw New Exception("Exceção no método 'controleContatoPrincipal'." & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region

#Region "Métodos privados"

    Private Sub alterarContato(ByVal iIDContato As Int32, _
                              ByVal iIDCliente As Int32, _
                              ByVal sNomeContato As String, _
                              ByVal iIDDepto As Int32, _
                              ByVal sTelefone As String, _
                              ByVal sCelular As String, _
                              ByVal sFax As String, _
                              ByVal sEmail As String, _
                              ByVal bPrincipal As Boolean)
        Try
            Me.objContato.alterarContato(iIDContato, _
                                         sNomeContato, _
                                         iIDCliente, _
                                         iIDDepto, _
                                         sTelefone, _
                                         sCelular, _
                                         sFax, _
                                         sEmail, _
                                         bPrincipal)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarContato'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Private Function inserirContato(ByVal sNomeContato As String, _
                                   ByVal iIdCliente As Int32, _
                                   ByVal iIdDepartamento As Int32, _
                                   ByVal sTelefone As String, _
                                   ByVal sCelular As String, _
                                   ByVal sFax As String, _
                                   ByVal sEmail As String, _
                                   ByVal bPrincipal As Boolean)
        Dim iIdContato As Int32

        Try
            iIdContato = Me.objContato.inserirContato(sNomeContato, _
                                                    iIdCliente, _
                                                    iIdDepartamento, _
                                                    sTelefone, _
                                                    sCelular, _
                                                    sFax, _
                                                    sEmail, _
                                                    bPrincipal)
            Return iIdContato

        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirContato'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region

End Class
